load("@rules_fuzzing//fuzzing:cc_defs.bzl", "cc_fuzz_test")
load("@properties//:properties.bzl", "PROPERTIES")

# Unless building for embedded systems all headers / functions should exist.
FAAD_DEFINES = [
    "APPLY_DRC",
    "HAVE_INTTYPES_H=1",
    "HAVE_MEMCPY=1",
    "HAVE_STRING_H=1",
    "HAVE_STRINGS_H=1",
    "HAVE_SYS_STAT_H=1",
    "HAVE_SYS_TYPES_H=1",
    "PACKAGE_VERSION=\\\"%s\\\"" % PROPERTIES["PACKAGE_VERSION"],
]

FAAD_SOURCES = glob([
    "libfaad/**/*.c",
    "libfaad/**/*.h",
])

FAAD_FLAGS = [
    "-Wall",
    "-pedantic",
]

DRM_AFFIX = [
    "",
    "_drm",
]

DRM_DEFINES = [
    [],
    ["DRM_SUPPORT"],
]

FIXED_AFFIX = [
    "",
    "_fixed",
]

FIXED_DEFINES = [
    [],
    ["FIXED_POINT"],
]

[cc_library(
    name = "faad" + DRM_AFFIX[drm] + FIXED_AFFIX[fixed],
    srcs = FAAD_SOURCES,
    hdrs = ["include/neaacdec.h"],
    copts = FAAD_FLAGS,
    includes = ["libfaad"],
    local_defines = FAAD_DEFINES + DRM_DEFINES[drm] + FIXED_DEFINES[fixed],
    strip_include_prefix = "include",
) for drm in range(2) for fixed in range(2)]

# To start fuzzing run: bazel run --config=asan-libfuzzer //:fuzz_config_run
cc_fuzz_test(
    name = "fuzz_config",
    srcs = ["fuzz/fuzz_config.c"],
    deps = [":faad"],
)

# To start fuzzing run: bazel run --config=asan-libfuzzer //:fuzz_decode_run
[cc_fuzz_test(
    name = "fuzz_decode" + DRM_AFFIX[drm] + FIXED_AFFIX[fixed],
    srcs = ["fuzz/fuzz_decode.c"],
    local_defines = DRM_DEFINES[drm],
    deps = [":faad" + DRM_AFFIX[drm] + FIXED_AFFIX[fixed]],
) for drm in range(2) for fixed in range(2)]

CLI_SOURCES = glob(["frontend/**/*.c", "frontend/**/*.h"], exclude = ["frontend/**/getopt.*"])

[cc_binary(
    name = "faad_cli" + DRM_AFFIX[drm] + FIXED_AFFIX[fixed],
    srcs = CLI_SOURCES,
    includes = ["frontend"],
    local_defines = FAAD_DEFINES + DRM_DEFINES[drm] + FIXED_DEFINES[fixed],
    deps = [":faad" + DRM_AFFIX[drm] + FIXED_AFFIX[fixed]],
) for drm in range(2) for fixed in range(2)]
